local Msg = require("core.Msg")
local PresentExcel = require("excel.present")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local BuyLogic = require("topup.BuyLogic")
local ItemDefine = require("bag.ItemDefine")

-- 登录推送
function onLogin(human)
    local msgRet = Msg.gc.GC_ACT_CUSTOMIZE_OPEN
    msgRet.isOpen = checkActIsOpen(human)
    Msg.send(msgRet,human.fd)
    if checkActIsOpen(human) == 1 then
        initHeroRiseDB(human)
    end
end

-- 检查活动是否开启
function checkActIsOpen(human)
    if human.db.lv < 15 then
        return 0
    end

    -- todo
    return 1
end

-- 初始化数据
function initCustomizeDB(human)
    if human.db.customize ~= nil then
        return
    end

    local now = os.time()
    human.db.customize = {}
    local config = PresentExcel.customizeGift
    local len = #config
    for i = 1,len do
        human.db.customize[i] = human.db.customize[i] or {}
        human.db.customize[i].cnt = 0
        human.db.customize[i].item = {}
        human.db.customize[i].selectItem = {}
    end
end

function customizeQuery(human)
    local isOpen = checkActIsOpen(human)
    if isOpen == 0 then
        return
    end

    if human.db.customize == nil then
        return
    end

    local msgRet = Msg.gc.GC_ACT_CUSTOMIZE_QUERY
    local config = PresentExcel.customizeGift
    local len = #config
    for i = 1,len do
        local v = config[i]
        local net = msgRet.giftList[i]
        net.id = i
        net.name = v.name
        net.limitCnt = v.limitCnt
        net.nowCnt = human.db.customize[i].cnt
        BuyLogic.fontBuyItem(human, net.buyItem, v.buyID)
        Grid.makeItem(net.zuanshi,ItemDefine.ITEM_ZUANSHI_ID,v.zuanshi)
        local cnt = 0
        for itemID,itemCnt in pairs(human.db.customize[i].item) do
            cnt = cnt + 1
            Grid.makeItem(net.item[cnt],itemID,itemCnt)
        end
        net.item[0] = cnt
        local warehouseLen = #v.first
        for j = 1,warehouseLen do
            local itemID = v.first[j][1]
            local itemCnt = v.first[j][2]
            Grid.makeItem(net.first[j],itemID,itemCnt)
        end
        net.first[0] = warehouseLen
        warehouseLen = #v.second
        for j = 1,warehouseLen do
            local itemID = v.second[j][1]
            local itemCnt = v.second[j][2]
            Grid.makeItem(net.second[j],itemID,itemCnt)
        end
        net.second[0] = warehouseLen
        warehouseLen = #v.third
        for j = 1,warehouseLen do
            local itemID = v.third[j][1]
            local itemCnt = v.third[j][2]
            Grid.makeItem(net.third[j],itemID,itemCnt)
        end
        net.third[0] = warehouseLen
    end
end

function selectItem(human,id,first,second,third)
    local msgRet = Msg.gc.GC_ACT_CUSTOMIZE_SELECT
    msgRet.ret = 1
    local isOpen = checkActIsOpen(human)
    if isOpen == 0 then
        msgRet.ret = 0
        Msg.send(msgRet,human.fd)
        return
    end

    if human.db.customize == nil then
        msgRet.ret = 0
        Msg.send(msgRet,human.fd)
        return
    end
    local config = PresentExcel.customizeGift[id]
    if config == nil 
       or config.first[first] == nil 
       or config.second[second] == nil 
       or config.third[third] == nil then
        msgRet.ret = 0
        Msg.send(msgRet,human.fd)
        return
    end

    human.db.customize[id].selectItem[1] = config.first[first]
    human.db.customize[id].selectItem[2] = config.second[second]
    human.db.customize[id].selectItem[3] = config.third[third]

    Msg.send(msgRet,human.fd)
end

function onBuyCustomize(human,id)
    local isOpen = checkActIsOpen(human)
    if isOpen == 0 then
        return
    end

    if human.db.customize == nil then
        return
    end

    if #human.db.customize[id].selectItem <= 0 then
        return
    end
    
    local config = PresentExcel.customizeGift[id]
    if human.db.customize[id].cnt >= config.limitCnt then
        return
    end

    human.db.customize[id].cnt = human.db.customize[id].cnt + 1
    if human.db.customize[id].cnt >= config.limitCnt then
        for i = 1,#human.db.customize[id].selectItem do
            local itemID = human.db.customize[id].selectItem[i][1]
            local itemCnt = human.db.customize[id].selectItem[i][2]
            human.db.customize[id].item[itemID] = itemCnt
        end
    end

 --[[   for i = 1,#human.db.customize[id].selectItem do
        local itemID = human.db.customize[id].selectItem[i][1]
        local itemCnt = human.db.customize[id].selectItem[i][2]
        BagLogic.addItem(human,itemID,itemCnt,"customize_gift")
    end
    ]]
    BagLogic.addItemList(human, human.db.customize[id].selectItem, "customize_gift")   
end